import unittest
import time
import subprocess
import tempfile
import os

class TestPerformance(unittest.TestCase):
    """性能测试"""
    
    def setUp(self):
        self.splitter_cmd = ['python3', '-m', 'tools.main']
    
    def test_cut_command_performance(self):
        """测试cut命令性能"""
        with tempfile.TemporaryDirectory() as tmpdir:
            output_dir = os.path.join(tmpdir, 'output')
            
            start_time = time.time()
            
            cmd = self.splitter_cmd + [
                'cut',
                '-r', '24.03-LTS',
                '-a', 'x86_64',
                '-o', output_dir,
                'bash_bin'
            ]
            
            result = subprocess.run(cmd, capture_output=True, text=True)
            end_time = time.time()
            
            execution_time = end_time - start_time
            
            # 验证在合理时间内完成（例如5分钟）
            self.assertLess(execution_time, 300, 
                          f"Cut command took too long: {execution_time} seconds")
            
            # 验证命令成功
            if result.returncode == 0:
                print(f"Cut command completed in {execution_time:.2f} seconds")
    
    def test_gen_command_performance(self):
        """测试gen命令性能"""
        with tempfile.TemporaryDirectory() as tmpdir:
            output_dir = os.path.join(tmpdir, 'sdf')
            os.makedirs(output_dir, exist_ok=True)
            
            start_time = time.time()
            
            cmd = self.splitter_cmd + [
                'gen',
                '-r', '24.03-LTS',
                '-o', output_dir,
                '-p', 'python3'
            ]
            
            result = subprocess.run(cmd, capture_output=True, text=True)
            end_time = time.time()
            
            execution_time = end_time - start_time
            
            # 验证在合理时间内完成（例如3分钟）
            self.assertLess(execution_time, 180,
                          f"Gen command took too long: {execution_time} seconds")
            
            print(f"Gen command completed in {execution_time:.2f} seconds")
    
    def test_memory_usage(self):
        """测试内存使用"""
        # 这里可以集成内存监控工具如memory_profiler
        # 目前作为占位符
        self.assertTrue(True)